/**
 * Created by Baggio on 2017-11-20.
 */
$(function () {

    getProduct();
     $(".amount_box a:eq(0)").click(function () {
        var amount = parseInt($(".amount_box input[type='text']").val());
        amount -= 1;
        if (amount < 1) {
            alert("数量最少是1！");
            return;
        }
        $(".amount_box  input[type='text']").val(amount);
    });
    $(".amount_box a:eq(1)").click(function () {
        var amount = parseInt($(".amount_box input[type='text']").val());
        amount += 1;
        $(".amount_box  input[type='text']").val(amount);
    });
    $(".button .btn1").click(function () {
        var userId=$("#userId").val();
        if(userId==undefined || userId=='' || userId==null){
          if( confirm("用户没有登录，是否登录？")){
              $(".login_box").show();
              $(".register_box1").hide();
              $(".message_box1").hide();
          }
            return;
        }

        var amount = parseInt($(".amount_box input[type='text']").val());
        var productJsons = [];
        for (var i = 0; i < amount; i++) {
            var productEntityId = $(".color_box .hover1").attr("data-id");
            if (productEntityId != '' && productEntityId != undefined) {
                productJson = {productEntityId: productEntityId, buyNum: 1};
                productJsons.push(productJson);
            }

            productEntityId = $(".package_box .hover1").attr("data-id");
            if (productEntityId != '' && productEntityId != undefined) {
                productJson = {productEntityId: productEntityId, buyNum: 1};
                productJsons.push(productJson);
            }


        }
console.log(productJsons);
        var postData = {productJsons:JSON.stringify(productJsons),source:1};var activityId=$("#activityId").val();
        if(activityId!='' && activityId!=undefined && activityId!=null){
            postData["activityId"]=activityId;
        }
        createOrder(postData);

    });
});
function createOrder(postData) {

    $.ajax({
        type: "post",
        url: "/order/createOrder.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }

            location.href = "/pay/" + res.data.orderId + ".html";
        }

    });
}
function getProduct( ) {
    var productId = $("#productId").val();
    var postData = {productId: productId}
    $.ajax({
        type: "post",
        url: "/product/getProduct.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            $("#title").html(data.productName);
            $(".purchase_details_2 div").html(data.productIntroduction);
            var colorHtml = "";


            for (var i = 0; i < data.productEntityList.length; i++) {
                if(data.productEntityList[i].status!=0){
                    colorHtml += '	<span ';
                    if(data.productEntityList[i].status==1){
                        colorHtml +=  ' onclick="setColor(' + data.productEntityList[i].productEntityId + ',' + i + ')" data-id="' + data.productEntityList[i].productEntityId + '"';

                    }
                      colorHtml += ' ><i class="circular" style="background:' + data.productEntityList[i].productEntityColourCode + '"></i>';
                    colorHtml += '&nbsp;&nbsp;';
                    colorHtml +=   data.productEntityList[i].productEntityColour;
                    colorHtml += '  </span>';
                }

            }
            $(".color_box").html(colorHtml);
            setColor(data.productEntityList[0].productEntityId, 0);
        }

    });
}
function setColor(productEntityId, index) {
    var postData = {productEntityId: productEntityId}
    var activityId=$("#activityId").val();
    if(activityId!='' && activityId!=undefined && activityId!=null){
        postData["activityId"]=activityId;
    }


    $.ajax({
        type: "post",
        url: "/productEntity/getProductEntity.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var price="";
            if(data.price>0){
                price= data.price / 100 + "元"
            }
            $(".price_1").find("a").html(price);
            $("#price").val(data.realPrice);
            $(".color_box span").removeClass();
            $(".color_box span").eq(index).addClass("hover1");
            //  $(".purchase_details_2 div").html(data.productEntityIntroduction);
            $(".slogan").html(data.productEntityIntroduction);
            $("#title").html(data.productEntityName);
            getProductPackage(data.productEntityId);
            setPic(data.productEntityId);
        }

    });

}
function setPic(productEntityId) {
    var postData = {resourceRelationId: productEntityId, resourceRelationType: 2}
    $.ajax({
        type: "post",
        url: "/resource/getResourceList.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var ul1 = $("#ul1");

            var data = res.data;
            var html = "";
            var pic = "";
            $("#ul1").html();
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    html += '<li class="lili1"><img class="lili_img" src="' + picPath + data[i].resourceUrl + '" alt="" /></li>';


                }
                pic = picPath + data[0].resourceUrl;

            }
            $("#ul1").html(html);


            $("#weixin_main_left1").find("img").attr('src', pic);
            var dd = "url(" + pic + ") no-repeat";
            $("#weixin_main_left2").css({"background": dd});
            $("#weixin_main_left2").css({"background-size": "200%"});



            var li1 = $(".lili1");
            li1.click(function(){
                var img1 = $(this).find(".lili_img").attr("src");
                $("#weixin_main_left1").find(".left_img").attr("src",img1);
                var img2 = "url(" + img1 + ") no-repeat";
                $("#weixin_main_left2").css({"background": img2 } );
                $("#weixin_main_left2").css({"background-size": "200%"});

            });
        }
    });
}

function getProductPackage(productEntityId) {
    var postData = {productEntityId: productEntityId}
    $.ajax({
        type: "post",
        url: "/product/getProductPackage.html",
        dataType: "json",
        async: false,
        data: postData,
        success: function (res) {
            console.log(res);
            if (res.code != 1) {
                alert(res.msg);
                return;
            }
            var data = res.data;
            var packageHtml = '';
            var realPrice = 0;




            if (data.length > 0) {

packageHtml='<h3 class="title1">套餐类型</h3><div class="package_box"> <div class="package_box">';
                for (var i = 0; i < data.length; i++) {
                    packageHtml += '  <span class="choice" data-id="' + data[i].packageId + '"  onclick="setPackage(' + data[i].realPrice + ',' + i + ')">' + data[i].packageName + '</span>';
                }

                realPrice = data[0].realPrice;
                packageHtml+=' </div>';
                console.log("data.length===>>>0==="+data.length)

            }
            $(".package").html(packageHtml);
            setPackage(realPrice, 0);
        }

    });
}
function setPackage(packagePrice, index) {
    var price = parseInt($("#price").val());
    price += parseInt(packagePrice);
    $(".price_2").find("a").html(price / 100 + "元");
    $(".package_box span").removeClass();
    $(".package_box span").eq(index).addClass("hover1");
}